/*
    Copyright (c);2019-2022;Wiscom System;

	All rights reserved.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the Wiscom System nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "los_sem.h"
#include "los_base.ph"
#include "los_hwi.h"
#include "los_api_irqres.h"
#include "los_inspect_entry.h"
#include "csg_sdk_hal.h"
#ifdef LOSCFG_LIB_LIBC
#include "string.h"
#endif

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cpluscplus */
#endif /* __cpluscplus */

static UINT32 g_uwTskHiID;
static UINT32 g_uwTskLoID;
#define TSK_PRIOR_HI 4
#define TSK_PRIOR_LO 5
static UINT32 temp_state;
static VOID LGPIO_Init(VOID)
{
    iomux_ls_iof_oval_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_0_IOF_OVAL,40,LGPIO_IO_PORT_PINS_0_HS_SEL,0,0);
    LGPIO_Output_Enable(LGPIO,1<<0);
    LGPIO_WriteBit(LGPIO,1<<0,0);
    LGPIO_Mode(LGPIO,1<<0,PP);
    iomux_ls_iof_oval_cfg(IOMUX_BASE,LGPIO_IO_PORT_PINS_1_IOF_OVAL,39,LGPIO_IO_PORT_PINS_1_HS_SEL,0,0);
    LGPIO_Output_Enable(LGPIO,1<<1);
    LGPIO_WriteBit(LGPIO,1<<1,0);
    LGPIO_Mode(LGPIO,1<<1,PP);
    return;
}
UINT8 test_step=0;
static UINT32 Example_TaskHi(VOID)
{
    UINT32 i;
    UINT32 Temp_data=0;
    temp_state=0;
    while(1)
    {
        Temp_data=(USART0->RXDATA);
        if(Temp_data& USART_RXDATA_EMPTY)
        {
            Temp_data=0;
        }
        else
        {
            Temp_data=(Temp_data & 0xFF);
        }
        if(test_step==0)
        {
            dprintf("\r\nsend '0xA1' to suspend taskHi!\r\n");
        }
        while(test_step==0)
        {
            Temp_data=(USART0->RXDATA);
            if(Temp_data& USART_RXDATA_EMPTY)
            {
                Temp_data=0;
            }
            else
            {
                Temp_data=(Temp_data & 0xFF);
            }
            if(Temp_data==0xA1)
            {
                dprintf("\r\nLOS_TaskSuspend(g_uwTskHiID);\r\n");
                test_step=1;
                temp_state=1;
                LGPIO_WriteBit(LGPIO,1<<0,0);
                LOS_TaskSuspend(g_uwTskHiID);
                LGPIO_WriteBit(LGPIO,1<<0,1);
                delay_1ms(2);
                LGPIO_WriteBit(LGPIO,1<<0,0);
            }
        }
        dprintf("\r\nsend '0xA2' to test again!\r\n");
        if(Temp_data==0xA2)
        {
            test_step=0;
        }
        dprintf("\r\nsend '0xA3' to finish test!\r\n");
        if(Temp_data==0xA3)
        {
            dprintf("\r\nTask switch test Finish!\r\n");
            LOS_InspectStatusSetByID(LOS_INSPECT_TSKSWT,LOS_INSPECT_STU_SUCCESS);
            test_step=2;
            Temp_data=0;
            LOS_TaskDelete(g_uwTskHiID);
            return LOS_OK;
        }
        Temp_data=0;
        LOS_TaskDelay(1000);
    }
}

static UINT32 Example_TaskLo(VOID)
{
    UINT32 Temp_data=0;
    UINT32 i;
    i=0;
    while(test_step<2)
    {
        if(temp_state==1)
        {
            dprintf("\r\nLOS_TaskResume!\r\n");
            LGPIO_WriteBit(LGPIO,1<<1,0);
            delay_1ms(500);
            LGPIO_WriteBit(LGPIO,1<<1,1);
            LOS_TaskResume(g_uwTskHiID);
            temp_state=0;
            test_step=0;
        }
        LOS_TaskDelay(100);
    }
    dprintf("\r\nTask Lo delete!\r\n");
    LOS_TaskDelete(g_uwTskLoID);
    return LOS_OK;
    LOS_TaskDelay(1000);
}
UINT32 Example_Tskswt(VOID)
{
    UINT32 uwRet;
    TSK_INIT_PARAM_S stInitParam;
    dprintf("\r\n--------4.16 Task switch test start!--------\r\n");
    /* lock task shcedue */
    LOS_TaskLock();

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_TaskHi;
    stInitParam.usTaskPrio = TSK_PRIOR_HI;
    stInitParam.pcName = "HIGH_NAME";
    stInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    /* create high prio task */
    uwRet = LOS_TaskCreate(&g_uwTskHiID, &stInitParam);
    if (uwRet != LOS_OK)
    {
        LOS_TaskUnlock();

        dprintf("\r\nExample_TaskHi create Failed!\r\n");
        return LOS_NOK;
    }

    dprintf("\r\n1. Example_TaskHi create Success!\r\n");

    stInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_TaskLo;
    stInitParam.usTaskPrio = TSK_PRIOR_LO;
    stInitParam.pcName = "LOW_NAME";
    stInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    /* create low prio task */
    uwRet = LOS_TaskCreate(&g_uwTskLoID, &stInitParam);
    if (uwRet != LOS_OK)
    {
        /* delete high prio task */
        if (LOS_OK != LOS_TaskDelete(g_uwTskHiID))
        {
            dprintf("\r\nTaskHi delete failed .\r\n");
        }

        LOS_TaskUnlock();

        dprintf("\r\nExample_TaskLo create Failed!\r\n");

        return LOS_NOK;
    }

    dprintf("\r\n2. Example_TaskLo create Success!\r\n");

    // dprintf("g_uwTskHiID %d \r\n",g_uwTskHiID);
    // dprintf("g_uwTskLoID %d \r\n",g_uwTskLoID);

    LGPIO_Init();
    
    dprintf("\r\n3. LGPIO init Success!\r\n");
    LOS_TaskUnlock();
    /* unlock task schedue */
    while(test_step<2)
    {
        LOS_TaskDelay(1000);
    }
    

    return uwRet;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cpluscplus */
#endif /* __cpluscplus */
